﻿using CrazyCancer.Framework.ShortUrl;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CrazyCancer.Framework.Communication.Interfaces;
using System;
using CrazyCancer.Framework.Communication;
using System.Configuration;

namespace CrazyCancerFramework.ShortUrl.Tests.VisualStudio
{
    [TestClass()]
    public class when_shortening_an_url_using_bitly
    {
        /// <summary>
        ///A test for ShortenUrl
        ///</summary>
        [TestMethod()]
        public void should_return_4b5LjC_for_cnn_with_user_name_and_password()
        {
            BitLy target = new BitLy(); 
            string longUrl = "http://www.cnn.com";
            string apiUrl = "http://api.bit.ly/shorten?version=2.0.1&format=xml&longUrl={0}&login={1}&apiKey={2}"; 
            ICommunicator communicator = new HttpCommunicator();
            string userName = GetUserName();                                     // TODO: Initialize to an appropriate value
            string password = GetPassword();                                     // TODO: Initialize to an appropriate value
            string expected = "http://bit.ly/4b5LjC"; 
            string actual;
            actual = target.ShortenUrl(longUrl, apiUrl, communicator, userName, password);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ShortenUrl
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void should_return_exception_for_cnn_without_credentials()
        {
            BitLy target = new BitLy(); 
            string longUrl = string.Empty; 
            string apiUrl = "http://api.bit.ly/shorten?version=2.0.1&format=xml&longUrl={0}&login={1}&apiKey={2}";
            ICommunicator communicator = new HttpCommunicator();
            string expected = "http://bit.ly/4b5LjC"; 
            string actual;
            actual = target.ShortenUrl(longUrl, apiUrl, communicator);
            Assert.AreEqual(expected, actual);
        }
        
        private string GetUserName()
        {
            return ConfigurationManager.AppSettings["bitLyUserName"].ToString();
        }

        private string GetPassword()
        {
            return ConfigurationManager.AppSettings["bitLyPassword"].ToString();
        }
    }
}
